﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;
using BabyShop.Site.Models;

namespace BabyShop.Site.Exts.Extensions
{
    public static class HtmlHelperExts
    {
        public static MvcHtmlString DropDownList(this HtmlHelper helper, string name, Type type)
        {
            if (!type.IsEnum) throw new ArgumentException("Type is not an enum.");
            var enums = (from int value in Enum.GetValues(type)
                         select new SelectListItem
                                    {
                                        Value = value.ToString(),
                                        Text = Enum.GetName(type, value)
                                    }).ToList();
            return System.Web.Mvc.Html.SelectExtensions.DropDownList(helper, name, enums);
        }

        public static MvcHtmlString DropDownList(this HtmlHelper helper, string name, Type type, object htmlAttributes)
        {
            if (!type.IsEnum) throw new ArgumentException("Type is not an enum.");
            var enums = (from int value in Enum.GetValues(type)
                         select new SelectListItem
                                    {
                                        Value = value.ToString(),
                                        Text = Enum.GetName(type, value)
                                    }).ToList();
            return System.Web.Mvc.Html.SelectExtensions.DropDownList(helper, name, enums, htmlAttributes);
        }

        public static MvcHtmlString DropDownList(this HtmlHelper helper, string name, Type type, string optionLabel, object htmlAttributes)
        {
            if (!type.IsEnum) throw new ArgumentException("Type is not an enum.");
            var enums = (from int value in Enum.GetValues(type)
                         select new SelectListItem {
                             Value = value.ToString(),
                             Text = Enum.GetName(type, value)
                         }).ToList();
            return System.Web.Mvc.Html.SelectExtensions.DropDownList(helper, name, enums, optionLabel, htmlAttributes);
        }

        public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression,
            Type type, object htmlAttributes)
        {
            if (!type.IsEnum) throw new ArgumentException("Type is not an enum.");
            var enums = (from int value in Enum.GetValues(type)
                         select new SelectListItem
                                    {
                                        Value = value.ToString(),
                                        Text = Enum.GetName(type, value)
                                    }).ToList();
            return System.Web.Mvc.Html.SelectExtensions.DropDownListFor(htmlHelper, expression, enums, htmlAttributes);
        }

        public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression,
            Type type)
        {
            if (!type.IsEnum) throw new ArgumentException("Type is not an enum.");
            var enums = (from int value in Enum.GetValues(type)
                         select new SelectListItem
                                    {
                                        Value = value.ToString(),
                                        Text = Enum.GetName(type, value)
                                    }).ToList();
            return System.Web.Mvc.Html.SelectExtensions.DropDownListFor(htmlHelper, expression, enums);
        }

        public static MvcHtmlString ActionLink(this HtmlHelper helper,ProductType productType)
        {
            return new MvcHtmlString(string.Format("<a href='{0}' title='{1}'>{1}</a>",
                                                   new UrlHelper(helper.ViewContext.RequestContext).Action(productType),
                                                   productType.ProductTypeName));
        }
    }

    public static class UrlHelperExts
    {
        public static string ConvertMenuTypeToUrl(this UrlHelper urlHelper, MenuType menuType)
        {
            switch (menuType) {
                case MenuType.NormalType:
                    return urlHelper.Action("SubCategory", "ProductType");
                case MenuType.CategoryType:
                    return urlHelper.Action("Category", "ProductType");
            }

            return string.Empty;
        }

        public static string Action(this UrlHelper urlHelper, ProductType productType)
        {
            if(productType == null) return string.Empty;
            return (string.IsNullOrEmpty(productType.MenuUrl)
                        ? urlHelper.ConvertMenuTypeToUrl(productType.MenuType)
                        : urlHelper.Content(productType.MenuUrl)) +
                   string.Format("?keyGuid={0}", productType.ProductTypeGuid);
        }
    }
}